Skip to content

first pass at implementing Lua df.new({assignment_table}) - #6039

Draft
SilasD wants to merge 1 commit into
DFHack:developfrom
SilasD:new-assign
Draft

SilasD wants to merge 1 commit into
DFHack:developfrom
SilasD:new-assign

Conversation

@SilasD

@SilasD SilasD commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Draft for discussion:
This PR adds an optional parameter to the Lua calls type:new(), ref:new(), and df.new(type).

This optional parameter is a table of data to be :assigned to the new object, using recursive table assignment.

type:new(table), ref:new(table), df.new(type, table)

Examples:

create a coord_path and assign data to it in one operation.

[lua]# b = df.coord_path:new{ x={1,2}, y={3,4}, z={5,6} }

[lua]# printall_recurse(b)
<coord_path: 0x1e8da5e6870>
x                       = <vector<int16_t>[2]: 0x1e8da5e6870>
    0                       = 1
    1                       = 2
y                       = <vector<int16_t>[2]: 0x1e8da5e6888>
    0                       = 3
    1                       = 4
z                       = <vector<int16_t>[2]: 0x1e8da5e68a0>
    0                       = 5
    1                       = 6

create a coord_path, copy the previous coord_path's data into it, then assign new data to it.

[lua]# c = b:new{ z={7,8} }

[lua]# printall_recurse(c)
<coord_path: 0x1e8da5e6550>
x                       = <vector<int16_t>[2]: 0x1e8da5e6550>
    0                       = 1
    1                       = 2
y                       = <vector<int16_t>[2]: 0x1e8da5e6568>
    0                       = 3
    1                       = 4
z                       = <vector<int16_t>[2]: 0x1e8da5e6580>
    0                       = 7
    1                       = 8

create a signed byte and assign a value to it.

[lua]# i = df.new("int8_t", { value = -123 })

[lua]# ~i
<int8_t: 0x1e8dadfaae0>
value                    = -123

create an stl string and assign a value to it.

[lua]# s = df.new("string", { value = "ABCDE" })

[lua]# ~s
<string: 0x1e8dbc0f500>
value                    = ABCDE

not yet implemented: assign data to a newly-created array.

memory leak: if the recursive table assignment fails,
the newly-created object leaks, as do any sub-objects
created before the assign fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant